package com.wang.leetcode;
//删除链表(链表已为升序)中的重复元素
public class Solution83 {
      public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
    //常规指针的做法
     public ListNode deleteDuplicates1(ListNode head) {
        ListNode p = head;
        if(head == null){
            return head;
        }
        while(p.next != null) {
            if(p.val == p.next.val) {
                p.next = p.next.next;
            }
            else{
                p = p.next;
            }
        }
        return head;
    }
    //递归
    public ListNode deleteDuplicates2(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        if (head.val == head.next.val) {
            head = deleteDuplicates2(head.next);
        } else {
            head.next = deleteDuplicates2(head.next);
        }
        return head;
    }
}
